package com.pb.camera.h264;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import u.aly.be;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.pb.camera.R;
import com.pb.camera.bean.TVideoFile;
import com.pb.camera.h264.VedioPlayView.SingleTapUp;

import com.pb.camera.utils.Logger;
import com.pb.camera.manager.EventManager;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.utils.TaskHelper.Task;
import com.pb.camera.view.ScalePanel;
import com.pb.camera.view.ScalePanel.OnValueChangeListener;
import com.pb.camera.view.ScalePanel.RecodeViewTime;
import com.pb.camera.work.ControlTask;
import com.pb.camera.work.IOTCInit;
import com.pb.camera.work.ReceiveTask;
import com.tutk.IOTC.AVIOCTRLDEFs;

public class PlayCurrentViewActivity extends Activity implements SingleTapUp,OnValueChangeListener,RecodeViewTime,OnClickListener,Observer{
	private static final String TAG = "PlayCurrentViewActivity";
	private VedioPlayView mVedioPlayView;
	//Jin Peng Hu
	//static final String UID = "YSS6XCZETL3FTPRH111A";
	static final String UID = "E4S7P4H8SZT9FSEB111A";
	private int sWidth;
	private int sHeight;
	
	private boolean isPlayOccurView = true;
	
	/**
	 * 时间刻度盘
	 */
	private ScalePanel sp_select_goback_view_time;
	List<TVideoFile> data = new ArrayList<TVideoFile>();
	
	private TextView tv_goback_view_date,tv_goback_view_time;
	private ImageView iv_left_day_view,iv_right_day_view;
	private LinearLayout ll_play_buttons,ll_talk_buttons,ll_date_buttons,ll_all;
	private ImageView iv_reverse_view,iv_mirror_view,iv_fill_screen_view;
	private TextView tv_select_view_qulity,tv_environment_model;
	private ImageView iv_no_sound,iv_talk,iv_photo_view;
	private View line_view;
	private int statusBarHeight;
	private ControlPlayBack mControlPlayBack;
	private int avIndexSuccess = 0;
	private int revertNum = 0;
	private int mirrorNum = 0;
	private int nightGoodNum = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_play_current_view);
		//获取屏幕的宽高
		WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		sWidth = wm.getDefaultDisplay().getWidth();
		sHeight = wm.getDefaultDisplay().getHeight();
		//获取状态栏的高度
		statusBarHeight = getStatusBarHeight();
		initFirst();
		initView();
		initData();
		showEnvironmentPopupwind();
		showViewQuityPopupwind();
	}
	private void initView() {
		mVedioPlayView = (VedioPlayView) findViewById(R.id.vedio_Play);
		
		ll_all = (LinearLayout) findViewById(R.id.ll_all);
		ll_play_buttons = (LinearLayout) findViewById(R.id.ll_play_buttons);
		ll_talk_buttons = (LinearLayout) findViewById(R.id.ll_talk_buttons);
		ll_date_buttons = (LinearLayout) findViewById(R.id.ll_date_buttons);
		
		iv_reverse_view = (ImageView) findViewById(R.id.iv_reverse_view);
		iv_mirror_view = (ImageView) findViewById(R.id.iv_mirror_view);
		tv_environment_model = (TextView) findViewById(R.id.tv_environment_model);
		tv_select_view_qulity = (TextView) findViewById(R.id.tv_select_view_qulity);
		iv_fill_screen_view = (ImageView) findViewById(R.id.iv_fill_screen_view);
		
		iv_no_sound = (ImageView) findViewById(R.id.iv_no_sound);
		iv_talk = (ImageView) findViewById(R.id.iv_talk);
		iv_photo_view = (ImageView) findViewById(R.id.iv_photo_view);
		
		sp_select_goback_view_time = (ScalePanel) findViewById(R.id.sp_select_goback_view_time);
		iv_left_day_view = (ImageView) findViewById(R.id.iv_left_day_view);
		iv_right_day_view = (ImageView) findViewById(R.id.iv_right_day_view);
		tv_goback_view_date = (TextView) findViewById(R.id.tv_goback_view_date);
		tv_goback_view_time = (TextView) findViewById(R.id.tv_goback_view_time);
		
		line_view = findViewById(R.id.line_view);
	}
	
	private void initData() {
		init = new IOTCInit(UID);
		//初始化摄像头平台
		startInitPlayView();
		mVedioPlayView.setIniter(init);
		mVedioPlayView.setFocusable(true);
		mVedioPlayView.setFocusableInTouchMode(true);
		mVedioPlayView.setOnMySingleTapUp(this);
		
		LayoutParams layoutParams = ll_all.getLayoutParams();
		layoutParams.height = sHeight - sWidth*3/4;
		layoutParams.width = sWidth;
		ll_all.setLayoutParams(layoutParams);
		ll_all.setY(sWidth * 3 / 4);
		
		sp_select_goback_view_time.setValueChangeListener(this);
		sp_select_goback_view_time.getRecodeViewTime(this);
		

		Calendar mCalendar = Calendar.getInstance();
		//设置时间块数据
		sp_select_goback_view_time.setTimeData(data);
		//设置当前时间
		sp_select_goback_view_time.setCalendar(mCalendar);
		
		iv_reverse_view.setOnClickListener(this);
		iv_mirror_view.setOnClickListener(this);
		tv_environment_model.setOnClickListener(this);
		tv_select_view_qulity.setOnClickListener(this);
		iv_fill_screen_view.setOnClickListener(this);
		ll_all.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		//开启接受设置结果
		ReceiveTask receiveTask = ReceiveTask.getDefault();
		receiveTask.startReceiveTask(avIndexSuccess);
		receiveTask.addObserver(this);
	}
	private void startInitPlayView() {
		TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
            	init.runInit();
            }
        });
	}
	private void initFirst() {
		ReceiveTask.getDefault().addObserver(this);
		for (int hourOffset = -5; Math.abs(hourOffset) <= 5; hourOffset++) {
			addTimeBloack(hourOffset);
		}
	}
	
	@Override
	public void onClick(View v) {
		//如果摄像头的通道打开
		if(avIndexSuccess >0 || avIndexSuccess == 0){
			switch (v.getId()) {
			case R.id.iv_reverse_view:
				swichRevertViewBackgound(revertNum);
				revertNum ++;
				if(revertNum == 2){
					revertNum = 0;
				}
				break;
			case R.id.iv_mirror_view:
				swichMirrorViewBackgound(mirrorNum);
				mirrorNum ++;
				if(mirrorNum == 2){
					mirrorNum = 0;
				}
				break;
			case R.id.tv_environment_model:
//				swichNightViewBackgound(nightGoodNum);
//				nightGoodNum ++;
//				if(nightGoodNum == 2){
//					nightGoodNum = 0;
//				}
				
				popEnvironment.showAsDropDown(v);
				break;
			case R.id.tv_select_view_qulity:
				popViewQuility.showAsDropDown(v);
				break;
			case R.id.iv_fill_screen_view:
				ll_all.setVisibility(View.GONE);
	//			ll_play_buttons.setVisibility(View.GONE);
	//			ll_talk_buttons.setVisibility(View.GONE);
	//			横屏
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
				mVedioPlayView.setFullScreen(true);
				//去除标题栏
				fullscreen(true);
				break;
			case R.id.tv_environment_model_indoor:
				beginSetEnvironment = tv_environment_model.getText().toString().trim();
				tv_environment_model.setText("室内");
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultIn = ControlTask.setEnviroment(0, (byte)AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_INDOOR_50HZ);
		            	if(resultIn){
		            		beginSetEnvironment = "室内";
		            	}
		            }
		        });
				popEnvironment.dismiss();
				break;
			case R.id.tv_environment_model_outdoor:
				beginSetEnvironment = tv_environment_model.getText().toString().trim();
				tv_environment_model.setText("室外");
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultOut = ControlTask.setEnviroment(0, (byte)AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_OUTDOOR);
						if(resultOut){
							beginSetEnvironment = "室外";
						}
		            }
		        });
				popEnvironment.dismiss();
				break;
			case R.id.tv_environment_model_night:
				beginSetEnvironment = tv_environment_model.getText().toString().trim();
				tv_environment_model.setText("夜间");
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultNight = ControlTask.setEnviroment(0, (byte)AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_NIGHT);
		            	Log.e("设置模式", "设置的结果是"+resultNight);
		            	if(resultNight){
							beginSetEnvironment = "夜间";
						}
		            }
		        });
				popEnvironment.dismiss();
				break;
			case R.id.tv_view_smooth:
				beginSetResolution = tv_select_view_qulity.getText().toString().trim();
				settingResoulution = "流畅";
				tv_select_view_qulity.setText(settingResoulution);
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultSmooth = ControlTask.setResolutionAction(0, (byte)AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN);
		            	mVedioPlayView.stopPlayView();
//						if(resultSmooth){
//							beginSetResolution = "流畅";
//							mVedioPlayView.setViewResolutionWithAndHegh(352, 288);
//							mVedioPlayView.startPlayView();
//						}
		            }
		        });
				popViewQuility.dismiss();
				break;
			case R.id.tv_view_standard:
				beginSetResolution = tv_select_view_qulity.getText().toString().trim();
				settingResoulution = "标清";
				tv_select_view_qulity.setText(settingResoulution);
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultModdle = ControlTask.setResolutionAction(0, (byte)AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIDDLE);
						Log.e("设置分辨", "设置的结果是"+resultModdle);
						mVedioPlayView.stopPlayView();
//		            	if(resultModdle){
//							beginSetResolution = "标准";
//							mVedioPlayView.setViewResolutionWithAndHegh(720, 480);
//							mVedioPlayView.startPlayView();
//						}
		            }
		        });
				popViewQuility.dismiss();
				break;
			case R.id.tv_view_high_defination:
				beginSetResolution = tv_select_view_qulity.getText().toString().trim();
				settingResoulution = "高清";
				tv_select_view_qulity.setText(settingResoulution);
//				mVedioPlayView.stopPlayView();
				TaskHelper.exec(new Runnable() {
		            @Override
		            public void run() {
		            	boolean resultHigh = ControlTask.setResolutionAction(0, (byte)AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX);
		            	mVedioPlayView.stopPlayView();
//						if(resultHigh){
//							beginSetResolution = "高清";
//							mVedioPlayView.setViewResolutionWithAndHegh(1280, 720);
//							mVedioPlayView.startPlayView();
//						}
		            }
		        });
				popViewQuility.dismiss();
				break;
	
			default:
				break;
			}
		}
	}
	
	private void swichNightViewBackgound(final int nightGoodNum2) {
		TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
            	if(nightGoodNum2 == 0){
            		ControlTask.setEnviroment(0, (byte)AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_INDOOR_50HZ);
        		}else{
        			ControlTask.setEnviroment(0, (byte)AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_NIGHT);
        		}
            }
        });
	}
	private void swichMirrorViewBackgound(final int mirrorNum2) {
		if(mirrorNum2 == 0){
			iv_mirror_view.setImageResource(R.drawable.view_mirror_noselect);
		}else{
			iv_mirror_view.setImageResource(R.drawable.view_mirror_select);
		}
		TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
            	if(mirrorNum2 == 0){
            		setAVMode(0,(byte)AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_NORMAL);
        		}else{
        			setAVMode(0,(byte)AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_MIRROR);
        		}
            }
        });
	}
	private void swichRevertViewBackgound(final int revertNum2) {
		if(revertNum2 == 0){
			iv_reverse_view.setImageResource(R.drawable.view_reverse_noselect);
		}else{
			iv_reverse_view.setImageResource(R.drawable.view_reverse_select);
		}
		TaskHelper.exec(new Runnable() {
            @Override
            public void run() {
            	if(revertNum2 == 0){
            		setAVMode(0,(byte)AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_NORMAL);
        		}else{
        			setAVMode(0,(byte)AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_FLIP);
        		}
            }
        });
	}
	private void addTimeBloack(int hourOffset) {
		TVideoFile file = new TVideoFile();
		Calendar startTime = Calendar.getInstance();
		startTime.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY) + hourOffset);
		startTime.set(Calendar.MINUTE, 0);
		file.startTime = startTime;

		Calendar endTime = Calendar.getInstance();
		endTime.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY) + hourOffset);
		endTime.set(Calendar.MINUTE, 50);
		file.endTime = endTime;
		data.add(file);
	}
	
	public String date2DateStr(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	public String date2timeStr(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		return dateFormat.format(date);
	}
	//下面两个是自定义直接获取时间日期等的字符串接口
	@Override
	public void onReceiveDate(String dataTime) {
		tv_goback_view_date.setText(dataTime);
	}
	@Override
	public void onReceiveTime(String hourTime) {
		tv_goback_view_time.setText(hourTime);
	}
	//下面两个是时间滑动,自己的接口
	@Override
	public void onValueChange(float value) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onValueChangeEnd(Calendar mCalendar) {
		// TODO Auto-generated method stub
		//这个的反映有点慢
//		String timeStr = date2timeStr(mCalendar.getTime());
//		String dateStr = date2DateStr(mCalendar.getTime());
//		tv_goback_view_time.setText(timeStr);
//		tv_goback_view_date.setText(dateStr);
	}
	//播放画面实现单击后,可以切换直播和回放
	@Override
	public void onMySingleTapUp() {
		// TODO Auto-generated method stub
		Log.e("huaiian", "画面被单击了LLLLLLLLLLLLLLLlll");
		if(isPlayOccurView){
			ll_play_buttons.setVisibility(View.GONE);
			ll_talk_buttons.setVisibility(View.GONE);
			
			sp_select_goback_view_time.setVisibility(View.VISIBLE);
			ll_date_buttons.setVisibility(View.VISIBLE);
			line_view.setVisibility(View.VISIBLE);
			tv_goback_view_time.setVisibility(View.VISIBLE);
			isPlayOccurView = false;
			if(mControlPlayBack == null) {
				mControlPlayBack = ControlPlayBack.getmDefault();
				mControlPlayBack.init(this,mVedioPlayView, sp_select_goback_view_time,iv_left_day_view,iv_right_day_view,
						tv_goback_view_date,tv_goback_view_time);
			}
		}else {
			ll_play_buttons.setVisibility(View.VISIBLE);
			ll_talk_buttons.setVisibility(View.VISIBLE);
			
			sp_select_goback_view_time.setVisibility(View.GONE);
			ll_date_buttons.setVisibility(View.GONE);
			line_view.setVisibility(View.GONE);
			tv_goback_view_time.setVisibility(View.GONE);
			isPlayOccurView = true;
			if(mControlPlayBack != null){
				mControlPlayBack.stopPlayRecord();
			}
		}
	}
	
	public int getStatusBarHeight() {
		  int result = 0;
		  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
		  if (resourceId > 0)
		      result = getResources().getDimensionPixelSize(resourceId);
		  return result;
	}
	//显示隐藏状态栏
	private void fullscreen(boolean enable) {
        if (enable) { //显示状态栏
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else { //隐藏状态栏
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(lp);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }
	
//	@Override
//	public void onConfigurationChanged(Configuration newConfig) {
//		super.onConfigurationChanged(newConfig);
//		 if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ){
//			//横屏
//			   
//		   }else if( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ){
//			//竖屏
//		   
//		   }
//	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ){
			 //横屏
			 ll_all.setVisibility(View.VISIBLE); 
			 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			 mVedioPlayView.setFullScreen(false);
			 //加上标题栏
			 fullscreen(false);
		 }else if( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ){
			 //竖屏
			 finish();
		 }
		return true;
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mControlPlayBack != null)
			mControlPlayBack.stopPlayRecord();
	}
	private PlayBackViewInterface playBackViewInterface;
	public interface PlayBackViewInterface {
		public void playBackViewControl(VedioPlayView mVedioPlayView, ScalePanel scalePanel,
										ImageView iv_left_day_view, ImageView iv_right_day_view,
										TextView tv_goback_view_date, TextView tv_goback_view_time);
	}
	
		private IOTCInit init;
		private PopupWindow popEnvironment;
		private TextView tv_environment_model_indoor;
		private TextView tv_environment_model_outdoor;
		private TextView tv_environment_model_night;
		private TextView tv_view_smooth;
		private TextView tv_view_standard;
		private TextView tv_view_high_defination;
		private String beginSetEnvironment = "室内";
		private String beginSetResolution = "流畅";
		private String settingResoulution = "流畅";
		private PopupWindow popViewQuility;
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		if(data instanceof AVIOCTRLDEFs.SMsgAVIoctrlPlayRecordResp && ((AVIOCTRLDEFs.SMsgAVIoctrlPlayRecordResp) data).command == AVIOCTRLDEFs.AVIOCTRL_RECORD_PLAY_END){
			//TODO: Play record end ,do something
			Logger.d(TAG,((AVIOCTRLDEFs.SMsgAVIoctrlPlayRecordResp) data).result+"");
		}
		if (data instanceof EventManager.InitFallEvent)
            initSetedData();
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlGetVideoModeResp) {
            switch (((AVIOCTRLDEFs.SMsgAVIoctrlGetVideoModeResp) data).mode) {
                case AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_FLIP:

                	Log.e("设置的反馈消息", "AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_FLIP");
//                    mCBRevert.setUnListenChecked(true);
                    break;
                case AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_MIRROR:
                	Log.e("设置的反馈消息", "AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_MIRROR");
//                    mCBMirror.setUnListenChecked(true);
                    break;
            }

        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlGetEnvironmentResp){
               if(((AVIOCTRLDEFs.SMsgAVIoctrlGetEnvironmentResp) data).mode == AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_NIGHT){
            	   tv_environment_model.setText("夜间");
            	   beginSetEnvironment = "夜间";
               }else if(((AVIOCTRLDEFs.SMsgAVIoctrlGetEnvironmentResp) data).mode == AVIOCTRLDEFs.AVIOCTRL_ENVIRONMENT_OUTDOOR){
            	   tv_environment_model.setText("室外");
            	   beginSetEnvironment = "室外";
               }else{
            	   tv_environment_model.setText("室内");
            	   beginSetEnvironment = "室内";
               }
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentResp){
        	if(((AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentResp) data).result != 0){
        		tv_environment_model.setText(beginSetEnvironment);
        	}else{
        		mVedioPlayView.stopPlayView();
        		switch (settingResoulution) {
				case "流畅":
					mVedioPlayView.setViewResolutionWithAndHegh(352, 288);
					break;
				case "标清":
					mVedioPlayView.setViewResolutionWithAndHegh(720, 480);
					break;
				case "高清":
					mVedioPlayView.setViewResolutionWithAndHegh(1280, 720);
					break;
				}
        		mVedioPlayView.startThreads();
        	}
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlGetStreamCtrlResp){
//        	mVedioPlayView.stopPlayView();
        	if(((AVIOCTRLDEFs.SMsgAVIoctrlGetStreamCtrlResp) data).quality == AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX){
        		tv_select_view_qulity.setText("高清");
        		beginSetEnvironment = "高清";
        		mVedioPlayView.setViewResolutionWithAndHegh(1280, 720);
        		Log.e("高清", "高清");
        	}else if(((AVIOCTRLDEFs.SMsgAVIoctrlGetStreamCtrlResp) data).quality == AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIDDLE){
        		tv_select_view_qulity.setText("标清");
        		beginSetEnvironment = "标清";
        		mVedioPlayView.setViewResolutionWithAndHegh(720, 480);
        		Log.e("标清", "标清");
        	}else{
        		tv_select_view_qulity.setText("流畅");
        		beginSetEnvironment = "流畅";
        		mVedioPlayView.setViewResolutionWithAndHegh(352, 288);
        		Log.e("流畅", "流畅");
        	}
        	mVedioPlayView.startThreads();
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlSetStreamCtrlResp){
        	
        	init.stopClient();
        	if(((AVIOCTRLDEFs.SMsgAVIoctrlSetStreamCtrlResp) data).result != 0){
        		tv_select_view_qulity.setText(beginSetResolution);
        	}else{
//        		mVedioPlayView.stopPlayView();
        		Log.e("目标画质", settingResoulution);
        		switch (settingResoulution) {
    			case "流畅":
    				mVedioPlayView.setViewResolutionWithAndHegh(352, 288);
    				Log.e("流畅", "流畅333333");
    				break;
    			case "标清":
    				mVedioPlayView.setViewResolutionWithAndHegh(720, 480);
    				Log.e("标清", "标清555");
    				break;
    			case "高清":
    				mVedioPlayView.setViewResolutionWithAndHegh(1280, 720);
    				Log.e("高清", "高清4444444444");
    				break;
    			}
        	}
        	startInitPlayView();
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeResp) {
            if (((AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeResp) data).result != 0){
//                Toast.makeText(getActivity(), R.string.set_failed, Toast.LENGTH_SHORT).show();
            }else{
            	Log.e("设置的反馈消息", "((AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeResp) data).result"+((AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeResp) data).result);
            	
            }
        }
	}
	//获取设置信息
	private void initSetedData() {
		// TODO Auto-generated method stub
		TaskHelper.exec(new Task() {
            boolean result;

            @Override
            public void execute() throws Exception {
                result = ControlTask.getVideoMode(0);
                ControlTask.getEnviroment(0);
                ControlTask.GetVideoSet(0);
                ControlTask.getResolution(0);
            }

            @Override
            public void callback(Exception e) {
//                if (!result)
//                    Toast.makeText(getActivity(), R.string.get_failed, Toast.LENGTH_SHORT).show();
//                Logger.d(TAG, result ? "Success" : "Failed");
            }
        });
	}
	//设置摄像头的几个功能 
	private void setAVMode(final int avIndex,final byte model) {
        TaskHelper.exec(new Task() {
            @Override
            public void execute() throws Exception {
                ControlTask.setVideoMode(avIndex,  model);
            }

            @Override
            public void callback(Exception e) {
            }
        });
    }
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mVedioPlayView.stopPlayView();
		init.stopClient();
	}
	
	private void showEnvironmentPopupwind() {
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 引入窗口配置文件
		View viewPop = inflater.inflate(R.layout.popupwind_select_environment, null);
		
		tv_environment_model_indoor = (TextView) viewPop.findViewById(R.id.tv_environment_model_indoor);
		tv_environment_model_outdoor = (TextView) viewPop.findViewById(R.id.tv_environment_model_outdoor);
		tv_environment_model_night = (TextView) viewPop.findViewById(R.id.tv_environment_model_night);
		popEnvironment = new PopupWindow(viewPop, sWidth/3-20, LayoutParams.WRAP_CONTENT);
		//设置点击窗口外边窗口消失
		popEnvironment.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		popEnvironment.setFocusable(true);
		popEnvironment.setBackgroundDrawable(new BitmapDrawable());
		
		tv_environment_model_indoor.setOnClickListener(this);
		tv_environment_model_outdoor.setOnClickListener(this);
		tv_environment_model_night.setOnClickListener(this);
	}
	
	private void showViewQuityPopupwind() {
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 引入窗口配置文件
		View viewPop = inflater.inflate(R.layout.popupwind_select_view_quity, null);
		
		tv_view_smooth = (TextView) viewPop.findViewById(R.id.tv_view_smooth);
		tv_view_standard = (TextView) viewPop.findViewById(R.id.tv_view_standard);
		tv_view_high_defination = (TextView) viewPop.findViewById(R.id.tv_view_high_defination);
		popViewQuility = new PopupWindow(viewPop, sWidth/3-20, LayoutParams.WRAP_CONTENT);
		//设置点击窗口外边窗口消失
		popViewQuility.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		popViewQuility.setFocusable(true);
		popViewQuility.setBackgroundDrawable(new BitmapDrawable());
		
		tv_view_smooth.setOnClickListener(this);
		tv_view_standard.setOnClickListener(this);
		tv_view_high_defination.setOnClickListener(this);
	}
}
